package com.yuven.baselib.widget;

import android.app.Dialog;
import android.content.Context;
import android.widget.TextView;

import com.yuven.baselib.R;
import com.yuven.baselib.utils.DialogUtil;

/**
 * 
 * 默认加载框实现
 *
 * @author :        Yuven
 * @date :          2020/8/29 9:46 PM
 * @desc :
 */
public class DefaultProgressSpinner implements ProgressSpinner {

    private Context context;

    private Dialog loadingAlertDialog;

    public DefaultProgressSpinner(Context context) {
        this.context = context;
    }


    @Override
    public void show(String msg, boolean cancelable) {
        try {
            if (loadingAlertDialog == null) {
                loadingAlertDialog = DialogUtil.loadingDialog(context, msg);
            } else {
                TextView tv = loadingAlertDialog.findViewById(R.id.text);
                if (tv != null) {
                    tv.setText(msg);
                }
            }
            loadingAlertDialog.setCancelable(cancelable);
            loadingAlertDialog.setCanceledOnTouchOutside(cancelable);
            loadingAlertDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dismiss() {
        if (isShowing()) {
            try {
                loadingAlertDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean isShowing() {
        return loadingAlertDialog == null ? false : loadingAlertDialog.isShowing();
    }


}
